<!--
 * @Description: 历史数据
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: Ronda
 * @LastEditTime: 2022-05-23 15:19:45
-->
<template>
  <div>
    <el-row>
      <el-col :span="4">
        <!-- 100vh-60px直接需要空格，不然效果出不来 100vh - 60px -->
        <div
          class="empData"
          style="
            padding-right: 10px;
            border-right: 1px solid #ccc;
            height: calc(100vh - 60px);
          "
        >
          <el-input placeholder="输入关键字进行过滤" v-model="filterText">
          </el-input>

          <el-tree
            class="filter-tree"
            :data="data"
            :props="defaultProps"
            :filter-node-method="filterNode"
            ref="tree"
            @node-click="handlerClick"
          >
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20">
        <div v-if="params.device_id" class="table" style="margin-left: 10px">
          <!-- 事件日期选择器和搜索按钮 -->
          <div class="header">
            <!-- value-format 将当前时间转换为时间戳 -->
            
            <el-date-picker
              value-format="timestamp"
              v-model="params.timer"
              type="datetimerange"
              :picker-options="pickerOptions"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              
              align="right"
            >
            </el-date-picker>
            
            <el-button type="primary" @click="toSearch">搜索</el-button>
          </div>
          <!-- 表格数据 -->
          <div class="content">
            <el-table :data="empData" style="width: 100%">
              <el-table-column prop="insert_time" label="日期" width="180">
                <template slot-scope="scope">
                  <div>
                    <!-- 全局时间过滤器的使用 -->
                    {{ scope.row.insert_time | fmtDate }}
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="tsp" label="tsp" width="180">
              </el-table-column>
              <el-table-column prop="pm10" label="pm10"> </el-table-column>
              <el-table-column prop="pm25" label="pm25"> </el-table-column>
              <el-table-column prop="temperature" label="温度">
              </el-table-column>
              <el-table-column prop="humidity" label="湿度"> </el-table-column>
              <el-table-column prop="wind_direction" label="风向">
              </el-table-column>
              <el-table-column prop="wind_speed" label="风速">
              </el-table-column>
            </el-table>
          </div>
        </div>
        <div v-else style="color: red; text-align: center">请选择设备</div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { get } from "@/utils/request";
export default {
  watch: {
    // 监听文本进行过滤
    filterText(val) {
      this.$refs.tree.filter(val);
    },
  },

  data() {
    return {
      // 过滤文本
      filterText: "",
      // 树型结构过滤源
      data: [],
      // 节点名称显示属性 树形结构配置项
      defaultProps: {
        children: "children",
        label: "name",
      },
      params: {
        page: 1,
        pageSize: 10,
        engineer_id: "",
        device_id: "",
      },
      // 事件日期选择器配置项
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 设备采集的数据
      empData: [],
    };
  },
  computed: {},
  methods: {
    // 去搜索
    toSearch(){
      // 发送请求
      this.getEmpData();
    },
    // 点击节点触发的函数
    // data 点击工程 data 是工程数据 有children
    // data 点击设备 data 是设备数据 无children
    handlerClick(data) {
      // data一级节点，value是二级节点数据
      // console.log(data);
      // 判断当前点击的是设备
      if (!data.children) {
        this.params.device_id = data.id;
        this.params.engineer_id = data.engineer_id;
      }
      this.getEmpData();
    },
    // 获取设备采集数据
    async getEmpData() {
      // params 参数 page pageSize engineer_id device_id timer
      // 判断时间是否被选择
      if(this.params.timer){
        this.params.begin_time = this.params.timer[0];
        this.params.end_time = this.params.timer[1];
        delete this.params.timer;
      }
      let res = await get("/monitor/pageQuery", this.params);
      // console.log(res);
      this.empData = res.data.list;
    },
    // 获取工程设备数据
    async loadEmpData() {
      let res = await get("/engineer/findEngineerDeviceTree");
      // console.log(res,'获取响应');
      this.data = res.data;
    },
    // 过滤节点方法，value是输入框输入的值 data是数据源
    filterNode(value, data) {
      if (!value) return true;
      // indexof 寻找字符方法，找到返回下标，找不到返回-1
      // return data.label.indexOf(value) !== -1;
      return data.name.indexOf(value) !== -1;
    },
  },
  created() {
    this.loadEmpData();
    this.getEmpData();
  },
  mounted() {},
};
</script>
<style scoped>
</style>